package com.itshidu.jiaowu.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itshidu.jiaowu.dao.*;
import com.itshidu.jiaowu.entity.*;
import com.itshidu.jiaowu.service.ClassesService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.itshidu.jiaowu.util.CookieUtils;
import com.itshidu.jiaowu.util.Result;
import com.itshidu.jiaowu.vo.ClassVo;
import com.itshidu.jiaowu.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author XiaZengming123
 * @since 2019-07-08
 */
@Service
@Primary
public class ClassesServiceImpl extends ServiceImpl<ClassesMapper, Classes> implements ClassesService {
    @Autowired UserMapper usermapper;
    @Autowired ClassesMapper classesmapper;
    @Autowired TeacherMapper teachermapper;
    @Autowired StudentClassesMapper studentClassesMapper;
    @Autowired StudentMapper studentMapper;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");


    @Override
    public Result TeacherSave(Long teacher, Long student, Long banteacher, Long maior, String classname, int stage, String start_date, String nd_date) {
        try {
            Classes c = classesmapper.selectByName(classname);
            if (c != null) {
                return Result.of(100, "班级名称已存在！");
            }
            Date sdate = sdf.parse(start_date);
            Date nddate = sdf.parse(nd_date);
            Classes classes = new Classes();
            classes.setName(classname);
            classes.setXueweiId(student);
            classes.setTeacherId(teacher);
            classes.setBanzhurenId(banteacher);
            classes.setMajorId(maior);
            classes.setStage(stage);
            classes.setStartDate(sdate);
            classes.setEndDate(nddate);
            classesmapper.insert(classes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.of(200);
    }

    //班次管理 查询所有班次信息
    @Override
    public Result list(int page, int limit) {

        PageHelper.startPage(page, limit);                                   //开始分页
        EntityWrapper<Classes> Classeswrapper = new EntityWrapper<>();      //mybatis的Wrapper接口
        List<Classes> list = classesmapper.selectList(Classeswrapper);       //查询所有
        PageInfo<Violate> info = new PageInfo(list);                      //pageinfo里包含分页信息
        List<ClassVo> list1 = new ArrayList();                                 //新建一个list集合
        for (Classes c : list) {
            //查讲师的名字
            Long Tid = c.getTeacherId();
            String Tname = teachermapper.SelectNameById(Tid);
            //查班长的名字
            Long Xid = c.getXueweiId();
            String Xname = teachermapper.SelectNameById(Xid);
            //查班主任的名字
            Long Bid = c.getBanzhurenId();
            String Bname = teachermapper.SelectNameById(Bid);


            //设置classvo对象
            ClassVo classvo = new ClassVo();
            classvo.setId(c.getId());
            classvo.setBanzhangname(Xname);
            classvo.setBanzhuren(Bname);
            classvo.setTeachername(Tname);
            classvo.setClassname(c.getName());
            classvo.setRoom(c.getRoom());
            classvo.setStage(c.getStage());
            classvo.setStartDate(c.getStartDate());
            classvo.setEndDate(c.getEndDate());
            list1.add(classvo);                      //存进list里
        }
        return Result.of().put("code", 0)
                .put("msg", "")
                .put("count", info.getTotal())
                .put("data", list1);
    }

    @Override
    public Result selectAllByClassName(int page,int limit,String key1) {

        Classes cla =classesmapper.selectByName(key1);
        PageHelper.startPage(page,limit);                                   //开始分页
        List<Student> list=studentMapper.SelectAllByClassName(cla.getId());
        System.out.println("--------+++"+list);
        PageInfo<Student> info = new PageInfo(list);
        List<StudentVo> list1=new ArrayList();
        for(Student s:list){
            Long Sid=s.getId();
            User user=usermapper.selectById(Sid);
            String sex=user.getSex();
            String name=user.getUsername();
            Long Cid=s.getClassid();
            Classes classes=classesmapper.selectById(Cid);
            Integer stage=classes.getStage();
            String nn=classes.getName();
            StudentVo sv=new StudentVo();
            sv.setId(s.getId());
            sv.setClassid(s.getClassid());
            sv.setUsername(name);
            sv.setSex(sex);
            sv.setStage(stage);
            sv.setClassesname(nn);
            list1.add(sv);
        }
        return Result.of().put("code", 0)
                .put("msg", "")
                .put("count",info.getTotal())
                .put("data", list1);
    }

    //班次管理- 给某个班级添加学生
    @Override
    public Result ClassAddStudent(Long[] arr, HttpServletRequest request) {

        String cookie =CookieUtils.getCookie(request, "classInfo");
        Long Classid=Long.valueOf(cookie);

        for (Long s : arr) {
            Student stu=studentMapper.SelectById(s);
            if (stu.getClassid().equals(Classid)){
                System.out.println("学生："+s+"已经在班级");
                return Result.of(201, "有选中的学生已经在班级了");
            } else {
                studentMapper.updateByClassid(s, Classid);
                studentClassesMapper.updateClassByClassid(s, Classid);
            }
        }
        return Result.of(200, "添加成功");
    }



}
